﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references

namespace com.qbengo.utility.nodepicker.DAL
{
    /// <summary>
    /// 数据访问类:VenueDao
    /// </summary>
    public partial class VenueDao
    {
        public VenueDao()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int venueid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from venue");
            strSql.Append(" where venueid=@venueid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@venueid", MySqlDbType.Int32)
			};
            parameters[0].Value = venueid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(com.qbengo.utility.nodepicker.entity.Venue model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into venue(");
            strSql.Append("name,`desc` as `desc`,lat,lng,thumbnail_url,`last-updated` as `last-updated`)");
            strSql.Append(" values (");
            strSql.Append("@name,@desc,@lat,@lng,@thumbnail_url,@last-updated)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.Text),
					new MySqlParameter("@desc", MySqlDbType.Text),
					new MySqlParameter("@lat", MySqlDbType.Double),
					new MySqlParameter("@lng", MySqlDbType.Double),
					new MySqlParameter("@thumbnail_url", MySqlDbType.Text),
					new MySqlParameter("@last-updated", MySqlDbType.DateTime)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.desc;
            parameters[2].Value = model.lat;
            parameters[3].Value = model.lng;
            parameters[4].Value = model.thumbnail_url;
            parameters[5].Value = model.last_updated;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(com.qbengo.utility.nodepicker.entity.Venue model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update venue set ");
            strSql.Append("name=@name,");
            strSql.Append("desc=@desc,");
            strSql.Append("lat=@lat,");
            strSql.Append("lng=@lng,");
            strSql.Append("thumbnail_url=@thumbnail_url,");
            strSql.Append("last-updated=@last-updated");
            strSql.Append(" where venueid=@venueid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.Text),
					new MySqlParameter("@desc", MySqlDbType.Text),
					new MySqlParameter("@lat", MySqlDbType.Double),
					new MySqlParameter("@lng", MySqlDbType.Double),
					new MySqlParameter("@thumbnail_url", MySqlDbType.Text),
					new MySqlParameter("@last-updated", MySqlDbType.DateTime),
					new MySqlParameter("@venueid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.desc;
            parameters[2].Value = model.lat;
            parameters[3].Value = model.lng;
            parameters[4].Value = model.thumbnail_url;
            parameters[5].Value = model.last_updated;
            parameters[6].Value = model.venueid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int venueid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from venue ");
            strSql.Append(" where venueid=@venueid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@venueid", MySqlDbType.Int32)
			};
            parameters[0].Value = venueid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string venueidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from venue ");
            strSql.Append(" where venueid in (" + venueidlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public com.qbengo.utility.nodepicker.entity.Venue GetModel(int venueid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select venueid,name,`desc` as `desc`,lat,lng,thumbnail_url,`last-updated` as `last-updated` from venue ");
            strSql.Append(" where venueid=@venueid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@venueid", MySqlDbType.Int32)
			};
            parameters[0].Value = venueid;

            com.qbengo.utility.nodepicker.entity.Venue model = new com.qbengo.utility.nodepicker.entity.Venue();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["venueid"] != null && ds.Tables[0].Rows[0]["venueid"].ToString() != "")
                {
                    model.venueid = int.Parse(ds.Tables[0].Rows[0]["venueid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["desc"] != null && ds.Tables[0].Rows[0]["desc"].ToString() != "")
                {
                    model.desc = ds.Tables[0].Rows[0]["desc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lat"] != null && ds.Tables[0].Rows[0]["lat"].ToString() != "")
                {
                    //model.lat=ds.Tables[0].Rows[0]["lat"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lng"] != null && ds.Tables[0].Rows[0]["lng"].ToString() != "")
                {
                    //model.lng=ds.Tables[0].Rows[0]["lng"].ToString();
                }
                if (ds.Tables[0].Rows[0]["thumbnail_url"] != null && ds.Tables[0].Rows[0]["thumbnail_url"].ToString() != "")
                {
                    model.thumbnail_url = ds.Tables[0].Rows[0]["thumbnail_url"].ToString();
                }
                if (ds.Tables[0].Rows[0]["last-updated"] != null && ds.Tables[0].Rows[0]["last-updated"].ToString() != "")
                {
                    model.last_updated = DateTime.Parse(ds.Tables[0].Rows[0]["last-updated"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select venueid,name,`desc` as `desc`,lat,lng,thumbnail_url,`last-updated` as `last-updated`");
            strSql.Append(" FROM venue ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM venue ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.venueid desc");
            }
            strSql.Append(")AS Row, T.*  from venue T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "venue";
            parameters[1].Value = "venueid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

